Frontend Forever App
We have a mobile app for you to download and use. And you can unlock many features in the app.
Get it now
Intall Later
Run
HTML
CSS
Javascript
Output
Document
Click the dark triangle(s) to replace them with smaller triangles
@charset "UTF-8"; @import url(https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700,800); *, :after, :before { box-sizing: border-box; padding: 0; margin: 0; } body { font-family: sans-serif; font-size: 16px; text-align: center; background: #fff; display: flex; height: 100vh; overflow: hidden; align-items: center; } main { width: 100%; text-align: center; } h1 { font-size: clamp(1rem, 2.5vw, 2rem); margin-bottom: 1rem; } svg { width: 80vmin; margin: 0 auto; } use { fill: #333; cursor: pointer; transition: 0.25s all; &:hover { fill: #4b4be7; } }
console.log("Event Fired") const addTriangle = (x, y, parent) => { const useElem = document.createElementNS("http://www.w3.org/2000/svg", "use"); useElem.setAttributeNS(null, "href", "#triangle"); useElem.setAttributeNS(null, "transform", "scale(0.5)"); if (x) useElem.setAttributeNS(null, "x", x); if (y) useElem.setAttributeNS(null, "y", y); parent.appendChild(useElem); }; const attachClickEvents = () => { // attach click events to all new triangles document.querySelectorAll("use:not([has-listener])").forEach((e) => { e.setAttribute("has-listener", true); // flag to prevent attaching new listeners to the same element e.addEventListener("click", () => { // create a new group and give it the same position as the clicked triangle const g = document.createElementNS("http://www.w3.org/2000/svg", "g"); if (e.parentElement.nodeName === "g") { g.setAttributeNS( null, "transform", `scale(0.5) translate(${e.getAttribute("x") ? e.getAttribute("x") : 0} ${ e.getAttribute("y") ? e.getAttribute("y") : 0 })` ); } // create the triangles and append them to the group element addTriangle(50, 0, g); addTriangle(0, 85, g); addTriangle(100, 85, g); // append the group element to the parent of the clicked element e.parentElement.appendChild(g); e.remove(); // remove the clicked element // attach click events to the new triangles attachClickEvents(); }); }); }; attachClickEvents();